package cn.zwxky.android.user.Search;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sobot.chat.SobotApi;
import com.sobot.chat.api.model.Information;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.LsfbFragment;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.zwxky.android.user.R;
import cn.zwxky.android.user.Utils.LittleUtils;
import cn.zwxky.android.user.Utils.Permission;
import cn.zwxky.android.user.Utils.URLString;

/**
 * Created by Administrator on 2017/3/21 0021.
 */

public class SearchFragment extends LsfbFragment {


    /**
     * view 对象
     */
    private View view;

    /**
     * activity对象
     */
    private Activity mactivity;

    private List<SearchBean.ListBean> mList;

    /**
     * 点击刷新布局控件
     */
    @ViewInject(R.id.swiplayout)
    private SwipeRefreshLayout swipeRefreshLayout;

    /**
     * list列表
     */
    @ViewInject(R.id.dish_list)
    private RecyclerView dish_list;

    @ViewInject(R.id.tv_badg)
    private TextView tv_badg;

    /**
     * RecyclerView的布局管理器
     */
    private RecyclerView.LayoutManager mlayoutManager;

    private SearchAdapter adapter;
    private Information info;

    private int type=0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_search, null);
        ViewInjectUtils.inject(this, view);
        LsfbEvent.getInstantiation().register2(this);
        Permission permission=new Permission(mactivity);
        permission.GetMeshPermission();
        permission.GetAddressBookPermission();
        permission.GetLocationPermission2();

        mList=new ArrayList<>();
        adapter=new SearchAdapter(mactivity,mList,R.layout.item_searh);
        mlayoutManager = new LinearLayoutManager(mactivity);//布局模式为listview方式
        dish_list.setLayoutManager(mlayoutManager);
        dish_list.setAdapter(adapter);

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
                type=1;
            }
        });

        info = new Information();

//        if (LittleUtils.badg.isEmpty() || LittleUtils.badg.equals("0")){
//            tv_badg.setVisibility(View.GONE);
//        }else {
//            tv_badg.setVisibility(View.VISIBLE);
//            tv_badg.setText(LittleUtils.badg);
//        }
        getData();
        type=0;
        return view;
    }

    /**
     * 用于存储activity对象
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mactivity = activity;
    }


    @Override
    public void onStart() {
        super.onStart();
        //获取未读消息数并显示
        String unreadNum = SobotApi.getUnreadMsg(mactivity.getApplicationContext()) > 0 ?
                SobotApi.getUnreadMsg(mactivity.getApplicationContext()) + "" : "";
        LsfbLog.e("获取未读消息数并显示"+unreadNum);
        if (unreadNum.isEmpty()){
            tv_badg.setVisibility(View.GONE);
        }else {
            tv_badg.setVisibility(View.VISIBLE);
            tv_badg.setText(unreadNum);
        }
    }

    /**
     * 点击事件
     * @param view
     */
    @OnClick({R.id.fg_add, R.id.fg_msg})
    public void onClick(View view) {
        switch (view.getId()) {
            //业务上传列表
            case R.id.fg_add:
              startActivity(new Intent(mactivity,BusinessAddListActivity.class));
                break;
            //消息
            case R.id.fg_msg:
                info.setAppkey("a14627c9117f47da9ab858fc55083aef");
                info.setUid(Config.getUserId());
                info.setUname(LittleUtils.nikename);
                info.setFace(LittleUtils.img_head);
                info.setTel(LittleUtils.phone);
                info.setColor("#374164");
                SobotApi.startSobotChat(mactivity, info);
                break;
        }
    }


    @Override
    protected void Resume() {
        type=0;
        getData();
        //获取未读消息数并显示
        String unreadNum = SobotApi.getUnreadMsg(mactivity.getApplicationContext()) > 0 ?
                SobotApi.getUnreadMsg(mactivity.getApplicationContext()) + "" : "";
        LsfbLog.e("获取未读消息数并显示"+unreadNum);
        if (unreadNum.isEmpty()){
            tv_badg.setVisibility(View.GONE);
        }else {
            tv_badg.setVisibility(View.VISIBLE);
            tv_badg.setText(unreadNum);
        }
    }

    @Override
    public void initData() {
    }

    public void getData(){
        HashMap<String, String> hashMap = new HashMap<String, String>();
           hashMap.put("uid", Config.getUserId());
        new BaseInternet().getData_Get(URLString.MYSERVICE, hashMap, SearchBean.class, new SearchEvent<SearchBean>(), true,true);
    }

    @EventAnnotation
    public void SearchEvent(SearchEvent<SearchBean> event){
        swipeRefreshLayout.setRefreshing(false);
        switch (event.getCode()){
            case 200:
                mList.clear();
                mList.addAll(event.getData().getList());
                adapter.notifyDataSetChanged();
                break;
            default:
                if (type==1) {
                    SN.SnackShow(mactivity.getWindow().getDecorView(), event.getMsg());
                }
                break;
        }
    }


    @EventAnnotation
    public void SearchClickEvent(SearchClickEvent event){
        startActivity(new Intent(mactivity,BusinessDetailsActivity.class).putExtra("id",event.getData()));
    }

    @Override
    public void onDestroy() {
        LsfbEvent.getInstantiation().unregister2(this);
        super.onDestroy();
    }

}
